package com.paomi.client.fragment.classify;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.paomi.client.R;
import com.paomi.client.activity.classify.SearchClassifyActivity;
import com.paomi.client.base.BaseLCEFragment;
import com.paomi.client.bean.UserInfo;
import com.paomi.client.fragment.classify.presenter.ClassifyFraPre;
import com.paomi.client.fragment.information.InformaticaFragment;
import com.paomi.client.fragment.shop.ShopFragment;
import com.paomi.client.fragment.show.ShowFragment;
import com.paomi.client.view.LoadingDialog;
import com.paomi.client.view.MyPopupMenu;
import com.paomi.client.widget.RxToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by pc on 2018/10/25
 * Describe:
 * com.paomi.client.fragment
 */
public class ClassifyFragment extends BaseLCEFragment<UserInfo, ClassifyFraPre> {
    @BindView(R.id.textview_title)
    TextView textviewTitle;
    @BindView(R.id.tv_search_icon)
    TextView tv_search_icon;
    @BindView(R.id.tv_search_clear)
    TextView tv_search_clear;
    @BindView(R.id.tv_classify_choose)
    TextView tv_classify_choose;
    @BindView(R.id.rb_information)
    RadioButton rb_information;
    @BindView(R.id.rb_shop)
    RadioButton rb_shop;
    @BindView(R.id.rb_show)
    RadioButton rb_show;
    @BindView(R.id.vp)
    ViewPager viewpager;
//    @BindView(R.id.et_search)
//    EditText etSearch;
    @BindView(R.id.et_search)
    TextView etSearch;
    @BindView(R.id.fab)
    FloatingActionButton fab;//悬浮框
    @BindView(R.id.null_net)
    RelativeLayout null_net;
    private boolean getData = false; //是否请求数据
    private int type = 0;

    private InformaticaFragment informaticaFragment;//资讯
    private ShopFragment shopFragment;//周边
    private ShowFragment showFragment;//演出
    private List<Fragment> fragments;


    public static ClassifyFragment newInstance(String name) {
        Bundle args = new Bundle();
        args.putString("name", name);
        ClassifyFragment fragment = new ClassifyFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @NonNull
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.fragment_classify;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        textviewTitle.setText(getResources().getString(R.string.title_classify));
        tv_search_icon.setTypeface(iconfont);
        tv_search_clear.setTypeface(iconfont);
        tv_classify_choose.setTypeface(iconfont);

//        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if ((actionId == 6) || (actionId == 2) || ((event != null) && (event.getAction() == 0) && (event.getKeyCode() == 66))) {
//                    if (etSearch.getText().toString() == null || etSearch.getText().toString().length() < 1) {
//                        RxToast.info(getResources().getString(R.string.search_null));
//                        return true;
//                    }
//                    getData = true;
//                    Bundle bundle = new Bundle();
//                    bundle.putString("getData", etSearch.getText().toString());
//                    bundle.putString("classify_id", "" + type);
//                    bundle.putBoolean("classify_get", getData);
//                    //演出
//                    bundle.putString("checkBox", "演出");
//                    showFragment.setArguments(bundle);
//                    showFragment.setListData();
//                    //周边
//                    bundle.putString("checkBox", "周边");
//                    shopFragment.setArguments(bundle);
//                    shopFragment.setListData();
//                    //资讯
//                    bundle.putString("checkBox", "资讯");
//                    informaticaFragment.setArguments(bundle);
//                    informaticaFragment.setListData();
//                    return true;
//                }
//                return false;
//            }
//        });
        if(getNetworkType()==0){//判断是否有网
            null_net.setVisibility(View.VISIBLE);
        }else{
            null_net.setVisibility(View.GONE);
            initFragment();
        }
    }

    private void initFragment() {
        Bundle bundle = new Bundle();
        bundle.putString("getData", etSearch.getText().toString());
        bundle.putString("classify_id", "1");
        bundle.putBoolean("classify_get", getData);
        informaticaFragment = new InformaticaFragment();
        informaticaFragment.setArguments(bundle);
        //周边
        shopFragment = new ShopFragment();
        shopFragment.setArguments(bundle);
        //演出
        showFragment = new ShowFragment();
        showFragment.setArguments(bundle);
        fragments = new ArrayList<Fragment>();
        fragments.add(showFragment);
        fragments.add(shopFragment);
        fragments.add(informaticaFragment);

        viewpager.setAdapter(new MyFragmentPagerAdapter(getActivity().getSupportFragmentManager(), fragments));

        // ViewPager页面切换监听
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        rb_show.setChecked(true);
                        break;
                    case 1:
                        rb_shop.setChecked(true);
                        break;
                    case 2:
                        rb_information.setChecked(true);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rb_information.setOnClickListener(mainListener);
        rb_shop.setOnClickListener(mainListener);
        rb_show.setOnClickListener(mainListener);

    }

    final View.OnClickListener mainListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.rb_show:
                    viewpager.setCurrentItem(0);
                    break;
                case R.id.rb_shop:
                    viewpager.setCurrentItem(1);
                    break;
                case R.id.rb_information:
                    viewpager.setCurrentItem(2);
                    break;
            }
        }
    };


    @NonNull
    @Override
    protected RecyclerView createRecycler() {
        return null;
    }

    @Override
    protected int getItemLayout() {
        return 0;
    }

    @Override
    protected void initEmpty(TextView empty) {

    }

    @Override
    protected void convert(BaseViewHolder helper, UserInfo item) {

    }

    @Override
    protected void onItemClick(View view, UserInfo item) {

    }

    @Override
    public ClassifyFraPre createPresenter() {
        return new ClassifyFraPre();
    }


    @OnClick({R.id.tv_search_clear,R.id.fab,R.id.et_search,R.id.go_search})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.tv_search_clear://清除搜索内容
                etSearch.setText("");
                break;
            case R.id.fab://悬浮框
                RxToast.info(getResources().getString(R.string.null_msg));
                break;
            case R.id.et_search://搜索
                getIntent(SearchClassifyActivity.class);
                break;
            case R.id.go_search://搜索
                getIntent(SearchClassifyActivity.class);
                break;
        }

    }



    public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> list;

        public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }


    MyPopupMenu myPopupMenu;

    @OnClick(R.id.tv_classify_choose)
    void choose() {
        myPopupMenu = new MyPopupMenu(this.getActivity());
        myPopupMenu.showLocation(R.id.tv_classify_choose);
        myPopupMenu.setOnClassifyClickListener(new MyPopupMenu.OnClassifyClickListener() {
            @Override
            public void onHotClick() {
                type = 1;
                getData = true;
                Bundle bundle = new Bundle();
                bundle.putString("getData", etSearch.getText().toString());
                bundle.putString("classify_id", "" + type);
                bundle.putBoolean("classify_get", getData);
                //演出
                bundle.putString("checkBox", "演出");
                showFragment.setArguments(bundle);
                showFragment.setListData();
                //周边
                bundle.putString("checkBox", "周边");
                shopFragment.setArguments(bundle);
                shopFragment.setListData();
                //资讯
                bundle.putString("checkBox", "资讯");
                informaticaFragment.setArguments(bundle);
                informaticaFragment.setListData();
                myPopupMenu.dismiss();
            }

            @Override
            public void onTimeClick() {
                type = 0;
                getData = true;
                Bundle bundle = new Bundle();
                bundle.putString("getData", etSearch.getText().toString());
                bundle.putString("classify_id", "" + type);
                bundle.putBoolean("classify_get", getData);
                //演出
                bundle.putString("checkBox", "演出");
                showFragment.setArguments(bundle);
                showFragment.setListData();
                //周边
                bundle.putString("checkBox", "周边");
                shopFragment.setArguments(bundle);
                shopFragment.setListData();
                //资讯
                bundle.putString("checkBox", "资讯");
                informaticaFragment.setArguments(bundle);
                informaticaFragment.setListData();
                myPopupMenu.dismiss();
            }
        });
    }

    @Override
    public void setData(List<UserInfo> data, boolean pullToRefresh) {
        super.setData(data, pullToRefresh);
       //移除FooterView
        getmAdapterWrapper().setLoadItemVisibility(false);
    }
    /**
     * 在无网的状态下重新加载
     */
    @OnClick(R.id.net_btn)
    public void onNetClicked() {
        LoadingDialog.get().showLoading();
        initFragment();
        if(getNetworkType()==0){//判断是否有网
            null_net.setVisibility(View.VISIBLE);
            LoadingDialog.get().hideLoading();
        }else{
            null_net.setVisibility(View.GONE);
        }
    }
}
